package com.pine.dao.pineframework.mapper;

import com.pine.common.database.BaseMapper;
import com.pine.dao.pineframework.entity.SysMenu;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


/**
 * <p>
 * 菜单表 Mapper 接口
 * </p>
 *
 * @author pine manage
 * @since 2024-08-12
 */
public interface SysMenuMapper extends BaseMapper<SysMenu> {

    String BASE_COLUMNS = "id, parent_id, `name`, menu_type, route_name, route_path, route_params, `component`, permission, `visible`, sort, icon, redirect, create_time, update_time, deleted";
    String M_BASE_COLUMNS = "m.id, m.parent_id, m.`name`, m.menu_type, m.route_name, m.route_path, m.route_params, m.`component`, m.permission, m.`visible`, m.sort, m.icon, m.redirect, m.create_time, m.update_time, m.deleted";


    /**
     * 递归查询部门和子部门
     */
    @Select("WITH RECURSIVE sub_menus AS (" +
            "SELECT " + BASE_COLUMNS + " FROM sys_menu WHERE id = #{menuId} and deleted = 0 " +
            "UNION ALL " +
            "SELECT " + M_BASE_COLUMNS + " FROM sys_menu m " +
            "INNER JOIN sub_menus sm ON sm.id = m.parent_id" +
            ") SELECT * FROM sub_menus;")
    List<SysMenu> listMenuAndChild(@Param("menuId") Long menuId);
}